﻿//------------------------------------------------------------------
// System Name:    NRemedy
// Component:      NRemedy
// Create by:      Parker Zhou (parkerz@wicresoft.com)
// Create Date:    2012-04-10
//------------------------------------------------------------------
using System;
using BMC.ARSystem;

namespace NRemedy
{
    public class ARLoginContext : IDisposable
    {
        public string Server { get; set; }

        public string User { get; set; }

        public string Password { get; set; }

        public string Authentication { get; set; }

        public IARServer ServerInstance { get; set; }

        public ARLoginStatus LoginStatus { get; set; }

        /// <summary>
        /// Constructor 1
        /// </summary>
        public ARLoginContext()
        { 
        }
        /// <summary>
        /// Constructor 2
        /// </summary>
        /// <param name="server">Server</param>
        /// <param name="user">User</param>
        /// <param name="password">Password</param>
        public ARLoginContext(string server, string user, string password)
        { 
            Init(server,user,password,string.Empty);
        }

        /// <summary>
        /// Init the server
        /// </summary>
        /// <param name="server">Server</param>
        /// <param name="user">User</param>
        /// <param name="password">Password</param>
        public void Init(string server, string user, string password)
        {
            Init(server,user,password,string.Empty);
        }

        /// <summary>
        /// Init the server
        /// </summary>
        /// <param name="server">Server</param>
        /// <param name="user">User</param>
        /// <param name="password">Password</param>
        /// <param name="authentication">Authentication</param>
        public void Init(string server, string user, string password, string authentication)
        {
            Server = server;
            User = user;
            Password = password;
            Authentication = authentication;
        }

        /// <summary>
        /// LogIn IARServer
        /// </summary>
        /// <param name="server">ARServer</param>
        public void Login(IARServer server)
        {
            if (LoginStatus != ARLoginStatus.Success || ServerInstance == null)
            {
                try
                {
                    server.Login(Server, User, Password, Authentication);
                    if (server.ServerAlive == true)
                    {
                        LoginStatus = ARLoginStatus.Success;
                        ServerInstance = server;
                    }
                    else
                    {
                        throw new Exception("log in failed.");
                    }
                }
                catch (Exception are)
                {
                    LoginStatus = ARLoginStatus.Failed;
                    throw are;
                }
            }
        }

        ~ARLoginContext()
        {
            Dispose();
        }

        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            if (ServerInstance != null)
                ServerInstance.Logout();
        }

        /// <summary>
        /// does have the enough parameters
        /// </summary>
        /// <returns></returns>
        public bool IsMinimunParameter()
        {
            return (!string.IsNullOrEmpty(Server)
                 && !string.IsNullOrEmpty(User)
                 && !string.IsNullOrEmpty(Password));
        }
    }

}
